﻿#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    init();//调用初始化函数
}

MainWindow::~MainWindow()
{
    delete ui;
}


//程序初始化函数,程序启动后最先调用此方法
void MainWindow::init()
{
    this->setCentralWidget(ui->stackedWidget);//设置界面栈为中心组件
    m_userModel = new QStandardItemModel(this);//实例化用户数据模型
    loadFile("userlist.txt");//加载用户数据模型

}

void MainWindow::loadFile(QString filePath)
{
    QFile file(filePath);//创建文件对象
    if(!file.exists()){//如果文件不存在,则发出通知
        QMessageBox::warning(this,"警告","没有找到用户数据文件,加载失败.");
        return;
    }

    //如果用户数据文件存在则读取该文件
    if(!file.open(QIODevice::ReadOnly)){//如果文件打开失败,则报错
        QMessageBox::warning(this,"警告","用户数据文件打开失败.");
        return;
    }

    //如果文件存在且打开成功
    m_userModel->clear();//清除原有数据
    m_userModel->setColumnCount(5);//设置用户模型为5列
    QTextStream stream(&file);//创建数据流对象
    stream.setCodec("UTF-8");//设置编码方式为utf8
    QStringList datas;
    for(;!stream.atEnd();){//读取文件
        datas = stream.readLine().split(' ',Qt::SkipEmptyParts);
        m_userModel->insertRow(m_userModel->rowCount());
        for(int i = 0;i < datas.count();++i)m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,i),datas.at(i));
    }
    file.close();
}

bool MainWindow::saceFile(QString filePath)
{
    QFile file(filePath);
    if(!file.open(QIODevice::WriteOnly)){
        return false;
    }
    QTextStream stream(&file);
    stream.setCodec("UTF-8");

    for(int i=0;i < m_userModel->rowCount();++i){
        QString line;
        for(int j=0;j < m_userModel->columnCount();++j){
            if(j)line+=' ';
            line +=m_userModel->data(m_userModel->index(i,j)).toString();
        }
        if(i!=m_userModel->rowCount()-1)line += '\n';
        stream << line;
    }
    file.close();

    return true;
}

void MainWindow::closeEvent(QCloseEvent *e)
{
    if(!saceFile("userlist.txt")){
        QMessageBox::warning(this,"警告","用户数据文件保存失败.");
    }
    e->accept();
}


void MainWindow::on_btnQuit_clicked()
{
    this->close();
    if(!saceFile("userlist.txt")){
        QMessageBox::warning(this,"警告","用户数据文件保存失败.");
    }
}


void MainWindow::on_btnNewAccount_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagNewAccount);

    ui->naEditName->clear();
    ui->naEditId->clear();
    ui->naEditPaws->clear();
    ui->naEditRePswd->clear();
}


void MainWindow::on_naBtnCancel_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void MainWindow::on_naBtnOk_clicked()
{
    QString name,id,pswd,repswd;
    name = ui->naEditName->text();
    id = ui->naEditId->text();
    pswd = ui->naEditPaws->text();
    repswd = ui->naEditRePswd->text();

    if(name.isEmpty()||id.isEmpty()||pswd.isEmpty()||repswd.isEmpty()){
        QMessageBox::warning(this,"警告","请输入完整的信息.");
        return;
    }

    if(id.count()!=18){
        QMessageBox::warning(this,"警告","请输入正确的身份证信息.");
        return;
    }

    if(pswd.count()!=6){
        QMessageBox::warning(this,"警告","请输入6位数字作为密码.");
        return;
    }

    if(pswd!=repswd){
        QMessageBox::warning(this,"警告","两次密码输入不一致.");
        return;
    }

    //检查该身份证是否已经使用过
    bool flag = true;
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,1))==id){
            flag = false;
            break;
        }
    }
    if(!flag){
        QMessageBox::warning(this,"警告","该身份证已经注册过银行卡.");
        return;
    }

    QString endid;
    for(;;){
        QString cardid = "622202";
        long first = QRandomGenerator(QTime::currentTime().second()).bounded(1000000,10000000);
        for(int i = 0;i < 6;++i){
            cardid += QString::number(first%10);
            first /= 10;
        }
        long second = QRandomGenerator(QTime::currentTime().msec()).bounded(1000000,10000000);
        for(int i = 0;i < 6;++i){
            cardid += QString::number(second%10);
            second /= 10;
        }
        bool flag = true;
        for(int i=0;i < m_userModel->rowCount();++i){
            if(m_userModel->data(m_userModel->index(i,2))==cardid){
                flag = false;
                break;
            }
        }
        if(flag){
            m_userModel->insertRow(m_userModel->rowCount());
            m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,0),name);
            m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,1),id);
            m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,2),cardid);
            m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,3),pswd);
            m_userModel->setData(m_userModel->index(m_userModel->rowCount()-1,4),"0");
            endid = cardid;
            break;
        }
    }

    QMessageBox::information(this,"提示","注册成功!\n姓名:"+name+"\n身份证:"+id+"\n卡号:"+endid);
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void MainWindow::on_btnDeleteAccount_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagDelete);
    ui->deleteEditCardid->clear();
    ui->deleteEditPswd->clear();
}


void MainWindow::on_deleteBtnCancel_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void MainWindow::on_deleteBtnOk_clicked()
{
    QString cardid,pswd;
    cardid = ui->deleteEditCardid->text();
    pswd = ui->deleteEditPswd->text();

    if(cardid.isEmpty()||pswd.isEmpty()){
        QMessageBox::warning(this,"警告","请输入完整的信息.");
        return;
    }


    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==cardid){
            if(m_userModel->data(m_userModel->index(i,3))==pswd){
                m_userModel->removeRow(i);
                QMessageBox::information(this,"提示","卡号:"+cardid+" 已注销.");
                ui->stackedWidget->setCurrentWidget(ui->pagMain);
                return;
            }else{
                QMessageBox::warning(this,"警告","密码错误.");
                return;
            }
        }
    }

    QMessageBox::warning(this,"警告","卡号错误.");
    return;
}


void MainWindow::on_pushButton_6_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void MainWindow::on_btnLogin_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagLog);
    ui->logEditCardid->clear();
    ui->logEditPswd->clear();
    m_mistime=0;
}


void MainWindow::on_pushButton_7_clicked()
{
    ui->stackedWidget->setCurrentWidget(ui->pagMain);
}


void MainWindow::on_pushButton_8_clicked()
{
    m_curCardId="";
    QString cardid,pswd;
    cardid = ui->logEditCardid->text();
    pswd = ui->logEditPswd->text();

    if(cardid.isEmpty()||pswd.isEmpty()){
        QMessageBox::warning(this,"警告","请输入完整的信息.");
        return;
    }


    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==cardid){
            if(m_userModel->data(m_userModel->index(i,3))==pswd){
                m_curCardId = cardid;
                ui->stackedWidget->setCurrentWidget(ui->pagLogin);
                return;
            }
        }
    }

    m_mistime++;
    if(m_mistime==3){
        QMessageBox::warning(this,"警告","密码输入错误次数已达到三次,请联系管理员.");
        this->close();
    }else{
        QMessageBox::warning(this,"警告","密码输入错误,"+QString::number(3-m_mistime)+"次后将会退出程序.");
    }
    return;

}


void MainWindow::on_pushButton_clicked()
{
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==m_curCardId){
            QMessageBox::information(this,"余额","您的余额为:"+m_userModel->data(m_userModel->index(i,4)).toString());
            return;
        }
    }
}


void MainWindow::on_pushButton_2_clicked()
{
    bool ok;
    int money = QInputDialog::getInt(this,"存钱","请输入存钱金额:(单笔最高100000)",1,1,100000,100,&ok);
    if(ok){
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==m_curCardId){
            m_userModel->setData(m_userModel->index(i,4),m_userModel->data(m_userModel->index(i,4)).toInt()+money);
            QMessageBox::information(this,"提示","操作成功!");
            return;
        }
    }
    }
}


void MainWindow::on_pushButton_3_clicked()
{
    bool ok;
    int money = QInputDialog::getInt(this,"取钱","请输入取钱金额:(单笔最高100000)",1,1,100000,100,&ok);
    if(ok){
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==m_curCardId){
            if(m_userModel->data(m_userModel->index(i,4)).toDouble()<money){
                QMessageBox::warning(this,"警告","余额不足.");
                return;
            }else{
                m_userModel->setData(m_userModel->index(i,4),m_userModel->data(m_userModel->index(i,4)).toInt()-money);
                QMessageBox::information(this,"提示","操作成功!");
                return;
            }
        }
    }
    }

}


void MainWindow::on_pushButton_5_clicked()
{
    bool ok;
    QString pswd = QInputDialog::getText(this,"修改密码","请输入6位新密码:",QLineEdit::Password,"",&ok);
    if(!ok)return;
    if(pswd.size()!=6){
        QMessageBox::warning(this,"警告","密码格式错误.");
        return;
    }

    QString repswd = QInputDialog::getText(this,"确认密码","请确认密码:",QLineEdit::Password);
    if(pswd!=repswd){
        QMessageBox::warning(this,"警告","密码不一致,修改失败.");
        return;
    }

    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==m_curCardId){
            m_userModel->setData(m_userModel->index(i,3),pswd);
            QMessageBox::information(this,"提示","操作成功!");
            return;
        }
    }
}


void MainWindow::on_pushButton_4_clicked()
{
    int myidrow;
    int existmoney=0;
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==m_curCardId){
            myidrow=i;
            existmoney=m_userModel->data(m_userModel->index(i,4)).toInt();
            break;
        }
    }

    bool ok;
    int money = QInputDialog::getInt(this,"转账金额","请输入转账金额:(单笔最高100000)",1,1,100000,100,&ok);
    if(ok){
        if(existmoney>=money){
            m_userModel->setData(m_userModel->index(myidrow,4),m_userModel->data(m_userModel->index(myidrow,4)).toInt()-money);
        }else{
            QMessageBox::warning(this,"警告","余额不足.");
            return;
        }
    }else{
        return;
    }


    QString peerid = QInputDialog::getText(this,"转账","请输入对方卡号:");
    for(int i=0;i < m_userModel->rowCount();++i){
        if(m_userModel->data(m_userModel->index(i,2))==peerid){
            m_userModel->setData(m_userModel->index(i,4),m_userModel->data(m_userModel->index(i,4)).toInt()+money);
            QMessageBox::information(this,"提示","操作成功!");
            return;
        }
    }

    QMessageBox::warning(this,"警告","未找到该用户.");
}

